package com.cars.util.file;


import org.apache.commons.codec.binary.Base64;

import java.io.*;

/**
 * Created by linan_leen on 2020-04-20
 * Description:
 */
public class PDFUtil {
    /**
     * 1 使用BASE64Decoder 对编码的字符串解码成字节数组
     * 2 使用底层输入流ByteArrayInputStream对象从字节数组中获取数据
     * 3 建立从底层输入流中读取数据的BufferedInputStream缓冲输出流对象
     * 4 使用BufferedOutputStream和FileOuptputSteam输出数据到指定的文件中
     * @param pdfBase64Str
     * @param filepath
     * @return
     */
    public static void BaseToPDF(String pdfBase64Str,String filepath) throws Exception{
        //将base64编码的字符串解码成字节数组
        byte[] bytes = Base64.decodeBase64(pdfBase64Str);

        //创建一个bytes作为其缓冲区的ByteArrayInputStream对象
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

        //创建从底层输入流中读取数据的缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(byteArrayInputStream);

        File file = new File(filepath);
        //创建到指定文件夹的输出流
        FileOutputStream fos = new FileOutputStream(file);

        //为文件输出流对接缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] buffer = new byte[1024];
        int length = bis.read(buffer);
        while (length!=-1){
            bos.write(buffer,0,length);
            length = bis.read(buffer);
        }
        bos.flush();

        try{
            bis.close();
            bos.close();
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try{
            PDFUtil.BaseToPDF("JVBERi0xLjUNCjQgMCBvYmoNCjw8L1R5cGUgL1BhZ2UvUGFyZW50IDMgMCBSL0NvbnRlbnRzIDUgMCBSL01lZGlhQm94IFswIDAgNTk1LjI5OTk4Nzc5IDg0MS45MDAwMjQ0MV0vUmVzb3VyY2VzPDwvRm9udDw8L0ZBQUFBSSA4IDAgUi9GQUFBQkIgMTEgMCBSL0ZBQUFCRyAxNiAwIFIvRkFBQUJKIDE5IDAgUi9GQUFBQ0IgMjEgMCBSL0ZBQUFDRyAyNiAwIFIvRkFBQUNJIDI4IDAgUj4+L1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